package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashSet;
import java.util.Set;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单管理
 */


@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Reference
    private UserService service;

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_LIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_LIST_FAIL);
        }
    }

    /**
     * 分页查询菜单
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 查询等级
     */
    @RequestMapping("/findByLevel")
    public Result findByLevel(Integer level){
        try {
            List<String> list = menuService.findByLevel(level);
            return new Result(true, MessageConstant.ADD_MENU_LIST_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_LIST_FAIL);
        }}

    @RequestMapping("/getMenus")
    public Result getMenu(String username){
        try {
            LinkedHashSet<Menu> set=new LinkedHashSet<>();
            User user = service.findByUserName(username);
            Set<Role> roles = user.getRoles();
            if(roles!=null&&roles.size()>0){
                for (Role role : roles) {
                    LinkedHashSet<Menu> menus = role.getMenus();
                    set.addAll(menus);
                }
            }
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,set);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true,MessageConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_FAIL);
        }
    }
    //编辑时回显数据
    @RequestMapping("/findById")
    public Result findById(String id){

        Result result = menuService.findById(id);
        return result;
    }
    //提交编辑的数据
    @RequestMapping("/edit")
    public Result editMenu(@RequestBody Menu menu){

        try {
            menuService.editMenu(menu);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
}
